#include "assglUtils.h"

#include <memory.h>
#include <iostream>

std::string ReadConfig(const char *filename)
{
    std::string data;
    FILE *f = fopen(filename, "r");
    if (f == nullptr)
    {
        std::cout << "open " << filename << " failed" << std::endl;
        return std::string("");
    }
    char buffer[1024];
    while (fgets(buffer, sizeof(buffer), f) != nullptr)
    {
        data.append(buffer);
        memset(buffer, 0, 1024);
    }
    fclose(f);
    return data;
}

int char_to_wchar(wchar_t *pDest, const char *pSrc)
{
	int len = 0;
    int ret = 0;
    setlocale(LC_CTYPE, "zh_CN.utf8");
    len = strlen(pSrc) + 1;

    if(len <= 1)
        return 0;

    ret = mbstowcs(pDest, pSrc, len);
    printf("%d\n",wcslen(pDest) );
    return ret;
}

float BezierTime = 0.0f; 
glm::vec2 Bezier2Point(glm::vec2 startPoint,glm::vec2 endPoint,glm::vec2 controlPoint)
{
    glm::vec2 point;
    BezierTime += 0.01f;
    if (BezierTime > 1.0f) BezierTime = 0.0f;

    float u = 1.0f - BezierTime; 

    point.x = u * u * startPoint.x + 2 * u * BezierTime * controlPoint.x + BezierTime * BezierTime * endPoint.x;
    point.y = u * u * startPoint.y + 2 * u * BezierTime * controlPoint.y + BezierTime * BezierTime * endPoint.y;
    return point;
}

long long timeStrFormatToMs(std::string timeFormat)
{
    long long ms = 0;
    std::string::size_type firstIndex = timeFormat.find(":",0);
    if (firstIndex == std::string::npos){
        ms = -1;
    }
    std::string tmp = timeFormat.substr(0,firstIndex);
    int h = std::stoi(tmp) ;
    tmp = timeFormat.substr(firstIndex+1,2);
    int m = std::stoi(tmp) ;
    tmp = timeFormat.substr(firstIndex+4,2);
    int s = std::stoi(tmp) ;
    int w = timeFormat.length()-(firstIndex+7);
    tmp = timeFormat.substr(firstIndex+7,w);
    int mS = std::stoi(tmp) ;
    
    if (ms != -1)
        ms = h*3600*1000 + m*60*1000 + s*1000 + mS*(w==1?100:(w==2?10:1));

	return ms;
}
